
import type { FormRules } from 'element-plus'
import { t } from "../../i18n/index"
import { phoneNumRuleSimple, taxpayerIdRule } from "../common"

// 店铺管理列表form表单数据格式
export class StoreManageListFormDataType {
    customerName:string = "" // 客户名称
    shopName:string = "" // 店铺名称
    contactPhone:string = "" // 店铺联系方式
    sortKey:string = "DESC" // 排序方式
}

// 店铺管理列表数据提交数据格式
export interface StoreManageListSubmitDataType {
    customerName: string; // 客户名称
    shopName: string; // 店铺名称
    contactPhone: string; // 联系方式
    contactName?: string;
    companyName?: string;
    orderBy?: string;
    orderByFiled?: string;
    sortKey?: string;
    shopType?: string;
    pageNo: number;
    pageSize: number;
}

// 基础信息的表单
export class BasicInformationFormDatatype {
    customerName: string = "" // 客户名称
    shopName: string = "" // 店铺名称
    businessCategory: string[][] = [['1', '3', '8'],['1', '3', '10']] // 经营类目
    shopType: string = "" // 店铺类型
    companyName: string = "" // 公司名称
    taxpayerId: string = "" // 纳税人识别号
    contactId: string = "" // 联系人Id
    contactName: string = "" // 联系人
    contactPhone: string = "" // 联系人电话
    businessLicenseImg: string = "" // 营业执照图
    businessHours: string[] = [] // 营业时间
}
// eslint-disable-next-line
enum TestStatus {
    // eslint-disable-next-line
    unuse = "unuse",
    // eslint-disable-next-line
    alreadyUsed = "alreadyUsed"
}
const testFn = () => {
    let flag = "unuse"
    return new Promise((resolve, reject) => {
        if (flag === TestStatus.unuse) {
            resolve(true)
        } else if (flag === TestStatus.alreadyUsed ) {
            resolve(false)
        } else {
            reject(false)
        }
    })
}

// 基础信息的表单校验
export const basicInformationFormRule: FormRules<BasicInformationFormDatatype> = {
    customerName: [
        // 请输入客户名称
        { required: true, message: t("clueInformation_customerName_placeholder"), trigger: 'blur' },
    ],
    shopName: [
        // 请输入店铺名称
        { required: true, message: t("storeManagement_storeName_placeholder"), trigger: 'blur' },
        {
            validator: function (rule: any, value: any, callback: any) {
                testFn().then((res) => {
                    if (!res) {
                        callback(new Error("用户已被占用"))
                    }else {
                        callback()
                    }
                }).catch(() => {
                    callback(new Error("校验失败"))
                })
            }, trigger: 'blur'
        },
    ],
    shopType: [
        { required: true, message: t('storeManagement_shopType_selectTips'), trigger: 'blur' },
    ],
    contactName: [
        { required: true, message: t('clueInformation_customerMoniker_placeholder'), trigger: 'blur' },
    ],
    contactPhone: phoneNumRuleSimple,
    companyName: [
        {
            validator: function (rule: any, value: any, callback: any) {
                testFn().then((res) => {
                    if (!res) {
                        callback(new Error("公司名称已存在，请仔细核对！"))
                    }else {
                        callback()
                    }
                }).catch(() => {
                    callback(new Error("校验失败"))
                })
            }, trigger: 'blur'
        },
    ],
    taxpayerId: [
        taxpayerIdRule,
        {
            validator: function (rule: any, value: any, callback: any) {
                testFn().then((res) => {
                    if (!res) {
                        callback(new Error("纳税人识别号已存在！"))
                    }else {
                        callback()
                    }
                }).catch(() => {
                    callback(new Error("校验失败"))
                })
            }, trigger: 'blur'
        },
    ],
    businessHours: [
        {
            // eslint-disable-next-line
            validator: function (rule: any, value: string[], callback: any){
                if (value.findIndex((item) => !item) !== -1) {
                    callback(new Error(t("storeManagement_businessHours_select")))
                }else {
                    callback()
                }
            },
            trigger: 'blur'
        }
    ]
}


// 店铺地址表单数据格式
export class StoreLocaltionFormDataType {
    contactAddress: string[] = [] // 联系地址
    detailedAddress: string = "" // 详细地址
}